<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="68px">
      <el-form-item label="生产单号" prop="produceNos">
        <el-input v-model="queryParams.produceNos" placeholder="请输入生产单号"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产品编码" prop="goodsCode">
        <el-input v-model="queryParams.goodsCode" placeholder="请输入产品编码"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产品名称" prop="goodsName">
        <el-input v-model="queryParams.goodsName" placeholder="请输入产品名称"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="生产日期">
        <el-date-picker v-model="dateRange"
                        style="display: flex;"
                        value-format="yyyy-MM-dd"
                        type="daterange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="产品型号" prop="goodsModel">
        <el-select v-model="queryParams.goodsModel" placeholder="请输入产品型号"
                   remote reserve-keyword :remote-method="searchGoods" filterable clearable>
          <el-option v-for="item in goodsList"
                     :key="item.id" :label="item.label" :value="item.model"/>
        </el-select>
      </el-form-item>
      <el-form-item label="操作工" prop="douser">
        <el-input v-model="queryParams.douser" placeholder="请输入操作工"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select clearable placeholder="选择状态" style="width: 200px" v-model="queryParams.status" size="mini">
          <el-option :key="dict.value" :label="dict.label" :value="dict.value"
                     v-for="dict in dict.type.produce_record_status"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" >导出
        </el-button>
      </el-form-item>
    </el-form>

    <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" ref="table"
              :max-height="tableMaxHeight">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" type="index"/>
      <el-table-column label="生产单号" align="center" prop="produceNos"/>
      <el-table-column label="产品编码" align="center" prop="goodsCode"/>
      <el-table-column label="产品名称" align="center" prop="goodsName"/>
      <el-table-column label="产品型号" align="center" prop="goodsModel"/>
      <el-table-column label="产品单位" align="center" prop="goodsUnit"/>
      <el-table-column label="数量" align="center" prop="count"/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.produce_record_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作工" align="center" prop="douser"/>
      <el-table-column label="自制入库明细" align="center" prop="doremark"/>
      <el-table-column label="退回原因" align="center" prop="refuseReason"/>
      <el-table-column label="生产日期" align="center" prop="produceDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.params.produceDate, '{y}-{m}-{d}') || '' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">

          <el-popconfirm :title="`确认入库退回当前数据项？`" @confirm="handleStoreReject(scope.row)"
                         v-if="scope.row.status === '2'" style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-circle-close"
                       v-hasPermi="['develop:ProduceRecord:store']">库管弃审
            </el-button>
          </el-popconfirm>

          <el-popconfirm :title="`确认入库退回当前数据项？`" @confirm="handleStoreReturn(scope.row)"
                         v-if="scope.row.status === '4'" style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-circle-close"
                       v-hasPermi="['develop:ProduceRecord:store']">入库退回
            </el-button>
          </el-popconfirm>

          <el-popconfirm :title="`确认通过当前数据项？`" @confirm="handleStoreIn(scope.row)"
                         v-if="scope.row.status === '6'" style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-download"
                       v-hasPermi="['develop:ProduceRecord:store']">库管入库
            </el-button>
          </el-popconfirm>

          <el-button size="mini" type="text" icon="el-icon-edit"
                     v-if="scope.row.status === '0' || scope.row.status === '3' || scope.row.status === '5'"
                     @click="handleUpdate(scope.row)" v-hasPermi="['develop:ProduceRecordDetail:edit']">修改
          </el-button>
          <el-popconfirm :title="`确认删除当前数据项？`" @confirm="handleDelete(scope.row)"
                         v-if="scope.row.status === '0' || scope.row.status === '3' || scope.row.status === '5'"
                         style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-delete"
                       v-hasPermi="['develop:ProduceRecordDetail:remove']">删除
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <produceRecordDetail-form ref="form" @ok="getList"></produceRecordDetail-form>
    <produce-record-detail-refuse-form ref="refuseForm" @ok="getList"></produce-record-detail-refuse-form>
    <produce-record-detail-refuse-store-form ref="refuseStoreForm"
                                             @ok="getList"></produce-record-detail-refuse-store-form>

  </div>
</template>

<script>
import {deleteAction, downloadFile, getAction, isEmpty} from '@/api/manage'
import ProduceRecordDetailForm from '@/views/develop/produceRecordDetail/module/ProduceRecordDetailForm'
import ProduceRecordDetailRefuseStoreForm
  from "@/views/develop/produceRecordDetail/module/ProduceRecordDetailRefuseStoreForm.vue";
import ProduceRecordDetailRefuseForm
  from "@/views/develop/produceRecordDetail/module/ProduceRecordDetailRefuseForm.vue";

export default {
  name: "ProduceRecordDetailList",
  dicts: ["produce_record_status"],
  components: {
    ProduceRecordDetailRefuseForm, ProduceRecordDetailRefuseStoreForm,
    ProduceRecordDetailForm,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产记录明细表格数据
      dataList: [],
      dateRange: [],
      goodsList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        produceId: null,
        produceNos: null,
        status: null,
        goodsId: null,
        goodsCode: null,
        goodsName: null,
        goodsModel: null,
        goodsUnit: null,
        count: null,
        douser: null,
        doremark: null,
        orderByColumn: 'a.produceNos',
        isAsc: "desc"
      },
      url: {
        list: '/produceRecordDetail/produceRecordDetail/list',
        remove: '/produceRecordDetail/produceRecordDetail/',
        export: '/produceRecordDetail/produceRecordDetail/export',
        storeReject: '/produceRecordDetail/produceRecordDetail/storeReject/',
        storeReturn: '/produceRecordDetail/produceRecordDetail/storeReturn/',
        storeIn: '/produceRecordDetail/produceRecordDetail/storeIn/',
      },
      tableMaxHeight: window.innerHeight - 320,
    };
  },
  created() {
    this.getList();
    window.addEventListener('resize', this.getTableHeight);
  },
  destroyed() {
    window.removeEventListener('resize', this.getTableHeight)
  },
  methods: {
    /** 查询生产记录明细列表 */
    getList() {
      this.loading = true;
      this.addDateRange(this.queryParams, this.dateRange, "ProduceDate");
      getAction(this.url.list, this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getTableHeight() {
      this.tableHeight = window.innerHeight - 320
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.form.handleAdd();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.form.handleUpdateTwice(row);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      deleteAction(this.url.remove, ids).then(response => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },

    handleStoreReject(row) {
      this.$refs.refuseForm.handleRefuse(row);
    },

    searchGoods(query) {
      if (query !== '') {
        let params = {"queryLabel": query, "pageNum": 1, "pageSize": 50,};
        getAction('/goodsOrderIn/goodsOrderIn/list', params).then(response => {
          this.goodsList = response.rows;
          for (let i = 0; i < this.goodsList.length; i++) {
            this.goodsList[i].label = this.goodsList[i].name + '-' + this.goodsList[i].model + '-' + this.goodsList[i].code;
          }
        })
      } else {
        this.goodsList = [];
      }
    },

    handleStoreReturn(row) {
      if (isEmpty(row.rukuDetailId)) {
        this.$modal.msgError("先入库该产品的生产单！");
        return;
      }
      this.$refs.refuseStoreForm.handleRefuse(row);
    },

    handleStoreIn(row) {
      getAction(this.url.storeIn + "/" + row.id, {}).then(response => {
        this.$modal.msgSuccess("操作成功");
        this.getList();
      }).catch(() => {
      });
    },

    handleExport(){
      let that = this;
      that.loading = false;
      that.$confirm('是否确认导出生产产品明细表?', "确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        downloadFile('/produceRecordDetail/produceRecordDetail/export', that.queryParams);
      });
    }


  }
};
</script>
